package cn.wyj.controller;

import cn.wyj.domain.Article;
import cn.wyj.domain.ArticleResponseData;
import cn.wyj.domain.Comment;
import cn.wyj.domain.StaticticsBo;
import cn.wyj.service.ArticleService;
import cn.wyj.service.SiteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private SiteService siteService;
    @Autowired
    private ArticleService articleService;

    /**
     * 跳转到后台首页
     * @param model
     * @return
     */
    @GetMapping(value = {"","/index"})
    public String index(Model model){
        List<Article> articleList = siteService.recentArticles(5);
        List<Comment> commentList = siteService.recentComments(5);
        StaticticsBo staticticsBo = siteService.getStatistic();
        model.addAttribute("comments",commentList);
        model.addAttribute("articles",articleList);
        model.addAttribute("statistics",staticticsBo);
        return "back/index";
    }

    /**
     * 跳转到文章发表页面
     * @return
     */
    @GetMapping("/article/toEditPage")
    public String toEditPage(){
        return "back/article_edit";
    }

    /**
     * 跳转到后台文章列表页面
     * @param currentPage
     * @param pageNum
     * @param model
     * @return
     */
    @GetMapping("/article")
    public String index(@RequestParam(value = "page",defaultValue = "1") Integer currentPage,
                        @RequestParam(value = "count",defaultValue = "10") Integer pageNum, Model model){
        PageInfo<Article> articles = articleService.findArticlesByPage(currentPage, pageNum);
        model.addAttribute("articles",articles);
        return "back/article_list";
    }

    /**
     * 发表文章
     * @param article
     */
    @PostMapping("/article")
    @ResponseBody
    public ArticleResponseData publishArticle(Article article){
        if(StringUtils.isBlank(article.getCategories())){
            article.setCategories("默认分类");
        }
        try{
            articleService.publishArticle(article);
            return ArticleResponseData.ok();
        }catch (Exception e){
            return ArticleResponseData.fail();
        }
    }

    /**
     * 带着文章数据跳转到修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/article/{id}")
    public String editArticle(@PathVariable("id") String id,Model model){
        Article article = articleService.findById(Integer.parseInt(id));
        model.addAttribute("contents",article);
        model.addAttribute("categories",article.getCategories());
        return "back/article_edit";
    }

    /**
     * 修改文章
     * @param article
     * @return
     */
    @PostMapping("/article/modify")
    @ResponseBody
    public ArticleResponseData modifyArticle(Article article){
        try{
            articleService.updateArticleById(article);
            return ArticleResponseData.ok();
        }catch (Exception e){
            return ArticleResponseData.fail();
        }
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @PostMapping("/article/delete")
    @ResponseBody
    public ArticleResponseData delArticle(Integer id){
        try{
            articleService.delArticle(id);
            return ArticleResponseData.ok();
        }catch (Exception e){
            return ArticleResponseData.fail();
        }
    }
}
